<template>
	<view class="content">
		<view class="list">
			<view class="tishi">若您忘记了密码，可在此重新设置新密码。</view>
			<view class="list-call">
				<image class="img" src="/static/shilu-login/1.png"></image>
				<input class="sl-input" type="number" v-model.trim="phone" maxlength="11" placeholder="请输入手机号" />
			</view>
			<view class="list-call">
				<image class="img" src="/static/shilu-login/2.png"></image>
				<input class="sl-input" type="text" v-model.trim="password" maxlength="32" placeholder="请输入新密码" :password="!showPassword" />
				<image class="img" :src="showPassword ? '/static/shilu-login/op.png' : '/static/shilu-login/cl.png'" @tap="display"></image>
			</view>
			<view class="list-call">
				<image class="img" src="/static/shilu-login/3.png"></image>
				<input class="sl-input" type="number" v-model.trim="code" maxlength="5" placeholder="验证码" />
				<view class="yzm" :class="{ yzms: second > 0 }" @tap="getcode">{{ yanzhengma }}</view>
			</view>
		</view>
		<view class="button-login" hover-class="button-hover" @tap="bindLogin()"><text>修改密码</text></view>
	</view>
</template>

<script>
import { getVerificationCode, updatePassword } from '@/api/user.js';

var _this, js;
export default {
	data() {
		return {
			phone: '',
			second: 0,
			code: '',
			showPassword: false,
			password: ''
		};
	},
	onLoad() {
		_this = this;
	},
	computed: {
		yanzhengma() {
			if (this.second == 0) {
				return '获取验证码';
			} else {
				if (this.second < 10) {
					return '重新获取0' + this.second;
				} else {
					return '重新获取' + this.second;
				}
			}
		}
	},
	onUnload() {
		this.clear();
	},
	methods: {
		async regUserPassWord(params) {
			const res = await updatePassword(params);
			if (res.flag) {
				return true;
			}
			
		},
		display() {
			this.showPassword = !this.showPassword;
		},
		clear() {
			clearInterval(js);
			js = null;
			this.second = 0;
		},
		async getcode() {
			let valid_rule = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;

			if (!valid_rule.test(this.phone)) {
				uni.showToast({
					icon: 'none',
					title: '请输入正确的手机号'
				});
				return;
			}

			const res = await getVerificationCode({ phone: this.phone });
			console.log(res);

			if (res.code === 200) {
				if (this.second > 0) {
					return;
				}
				this.second = 60;
				//请求业务
				js = setInterval(function() {
					_this.second--;
					if (_this.second == 0) {
						_this.clear();
					}
				}, 1000);
			}

			uni.showToast({
				title: res.message,
				icon: 'none'
			});
		},
		bindLogin() {
			if (this.password.length < 6 && this.password.length > 12) {
				uni.showToast({
					icon: 'none',
					title: '密码长度须大于6位且小于12位'
				});
				return;
			}

			if (this.code.length != 5) {
				uni.showToast({
					icon: 'none',
					title: '验证码不正确'
				});
				return;
			}

			const params = {
				phone: this.phone,
				password: this.password.encryptLong(),
				verificationCode: this.code
			};

			const flag = this.regUserPassWord(params);
			if (flag) {
				uni.showToast({
					title: '修改密码成功',
					success: () => {
						uni.redirectTo({
							url: '/pages/login/login?params=' + JSON.stringify({phone:this.phone})
						});
					}
				});
			}
		}
	}
};
</script>

<style>
.content {
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	height: 100vh;
	width: 750rpx;
	position: fixed;
	top: 0;
	left: 0;
}

.tishi {
	color: #999999;
	font-size: 28rpx;
	line-height: 50rpx;
	margin-bottom: 50rpx;
}

.list {
	display: flex;
	flex-direction: column;
	padding-top: 50rpx;
	padding-left: 70rpx;
	padding-right: 70rpx;
}

.list-call {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
	height: 100rpx;
	color: #333333;
	border-bottom: 0.5px solid #e2e2e2;
}

.list-call .img {
	width: 40rpx;
	height: 40rpx;
}

.list-call .sl-input {
	flex: 1;
	text-align: left;
	font-size: 32rpx;
	margin-left: 16rpx;
}

.button-login {
	color: #ffffff;
	font-size: 34rpx;
	width: 470rpx;
	height: 100rpx;
	background: linear-gradient(-90deg, rgba(63, 205, 235, 1), rgba(188, 226, 158, 1));
	box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.2);
	border-radius: 50rpx;
	line-height: 100rpx;
	text-align: center;
	margin-left: auto;
	margin-right: auto;
	margin-top: 100rpx;
}

.button-hover {
	background: linear-gradient(-90deg, rgba(63, 205, 235, 0.8), rgba(188, 226, 158, 0.8));
}

.yzm {
	color: #ff7d13;
	font-size: 24rpx;
	line-height: 64rpx;
	padding-left: 10rpx;
	padding-right: 10rpx;
	width: auto;
	height: 64rpx;
	border: 1rpx solid rgba(255, 131, 30, 1);
	border-radius: 50rpx;
}

.yzms {
	color: #999999 !important;
	border: 1rpx solid #999999;
}
</style>
